<%@ page import="com.xyc.mapper.UserMapper" %>
<%@ page import="com.xyc.pojo.User" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="org.springframework.context.support.ClassPathXmlApplicationContext" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理</title>
    <link href="css/index.css" rel="stylesheet" type="text/css"/>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <style>
        table, tr, td {
            border: 1px solid #ccc;
        }

        table {
            text-align: center;
        }

        .row1 td, .row2 td, .row_hover td, .paging_row td {
            padding: 5px 10px;
            color: #666666;
            border: 1px solid #CBD6DE;
        }

        .row2 td {
            background: #ffffff;
        }

        .row1 td {
            background: #eeeeee;
        }

        .row_hover td {
            background: #FBFACE;
            color: #000000;
        }

        a {
            text-decoration: none;
        }
    </style>
</head>
<body>
<div>
    <div id="top">
        <p class="title">&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="index.jsp" style="cursor:pointer;">HappyMusic后台系统</a>
        </p>
        <p class="quit">&nbsp;<a href="login.html" onclick="return confirm('确认退出吗? ');quit()" id="quit">退出</a>&nbsp;&nbsp;&nbsp;
        </p>
        <p class="welcome">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;欢迎你&nbsp;<%=session.getAttribute("username")%>&nbsp;&nbsp;&nbsp;</p>
        <p id="showTime" class="showTime">&nbsp;&nbsp;</p>
    </div>
    <div id="left">
        <div class="rad"><a href="admin">系统设置</a></div>
        <div class="rad"><a href="myUser">用户管理</a></div>
        <div class="rad"><a href="mySong">歌曲管理</a></div>
        <div class="rad"><a href="myList">歌单管理</a></div>
    </div>
    <div id="right">
        <!--显示系统设置的内容-->
        <p>&nbsp;&nbsp;欢迎进入HappyMusic后台管理系统</p>
        <table>
            <tr>
                <%--  <td>编号</td>--%>
                <td>昵称</td>
                <td>性别</td>
                <td>年龄</td>
                <td>邮箱</td>
                <td>会员</td>
                <td>可用</td>
                <td>会员开通</td>
                <td>会员到期</td>
                <td>操作</td>
            </tr>
            <%
                String n = request.getParameter("pageNow");
                int pageSize = 5;
                int lineCount = (int) session.getAttribute("myUserCount");
                int pageCount = lineCount % pageSize == 0 ? lineCount / pageSize : lineCount / pageSize + 1;
                int pageNow = 1;
                if (n != null) {
                    pageNow = Integer.parseInt(n);
                }
            %>
            <br/>&nbsp;&nbsp;点击页码
            <%
                for (int i = 1; i <= pageCount; i++) {%>
            <a href="myUser?pageNow=<%=i%>" style="text-decoration: none;"><%=i%>
            </a>&nbsp;&nbsp;
            <%}%>
            <br/><br/>
            <div>
                <form method="post" action="search">
                    <span><input type="text" placeholder="查找内容" value="" name="searchInfo" id="sear"/></span>
                    <span><input type="submit" value="查找"/></span>
                    <span id="tips"></span>
                </form>
            </div>
            <br/><br/>
            <%
                ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml");
                UserMapper mapper = ctx.getBean("userMapper", UserMapper.class);
                int a = pageSize * (pageNow - 1);
                int b = pageSize;
                List<User> userListLimit = mapper.userLimit(a + "" + "," + b);
                for (int i = 0; i < userListLimit.size(); i++) {
                    User user = userListLimit.get(i);
            %>
            <tr class="row<%=i%2+1%>">
                <%-- <td><%=user.getUser_id()%>
                 </td>--%>
                <td><%=user.getUser_name()%>
                </td>
                <td><%=user.getSex()%>
                </td>
                <td><%=user.getAge()%>
                </td>
                <td><%=user.getEmail()%>
                </td>
                <td><%=user.getIsVip()%>
                </td>
                <td><%=user.getIsUse()%>
                </td>
                <%
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String start = sdf.format(user.getCreate_time());
                    String end = sdf.format(user.getLast_time());
                %>
                <td><%=start%>
                </td>
                <td><%=end%>
                </td>
                <td>
                    <a href="findUser?id_update=<%=user.getUser_id()%>">修改</a>
                    <a href="deleteUser?id_delete=<%=user.getUser_id()%>"
                       onclick="return confirm('确认删除<%=user.getUser_name()%>吗? ')">删除</a>
                    <a href="addUser">添加</a>
                </td>
            </tr>
            <%}%>
        </table>


    </div>
    <div id="bottom">版权所有</div>
</div>
<script>
    function showTime() {
        var a = new Date();
        var b = a.toLocaleTimeString();
        var c = a.toLocaleDateString();
        document.getElementById("showTime").innerHTML = c + "&nbsp" + b;
    }

    setInterval(function () {
        showTime()
    }, 1000);

    function quit() {
        //防止页面后退
        history.pushState(null, null, document.URL);
        window.addEventListener('popstate', function () {
            history.pushState(null, null, document.URL);
        });
    }

    $('#sear').blur(function () {
        var data = $('#sear').val();
        if (data == null || data == "") {
            $('#tips').text("内容为空!");
            $('#tips').css("color", "red");
        } else {
            $('#tips').text(" ");
        }
    });
</script>
</body>
</html>